﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ToolPositioning.CApTPObjectValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ToolPositioning;

internal class CApTPObjectValidator : ITxValidator
{
  internal Func<ITxObject, bool> IsUniqueObjectPicked;
  private ITxTypeFilter _filter;
  private string _invalidObjMessage;
  private string _missingObjMessage;
  private string _duplicateErrorMessage;

  public CApTPObjectValidator(
    Func<ITxObject, bool> icUnique,
    string missingObj,
    string invalidObj,
    string duplicateObj)
  {
    this.IsUniqueObjectPicked = icUnique;
    this._filter = (ITxTypeFilter) new TxTypeFilter(typeof (ITxComponent));
    this._invalidObjMessage = invalidObj;
    this._missingObjMessage = missingObj;
    this._duplicateErrorMessage = duplicateObj;
  }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject = (ITxObject) null;
    TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
    for (int index = 0; index < ((Collection<ITxObject>) objectsByName).Count && itxObject == null; ++index)
    {
      if (this._filter.DoesPassFilter(((Collection<ITxObject>) objectsByName)[index]) && this.IsUniqueObjectPicked(((Collection<ITxObject>) objectsByName)[index]))
        itxObject = ((Collection<ITxObject>) objectsByName)[index];
    }
    return itxObject;
  }

  public string GetText(ITxObject obj)
  {
    string text = (string) null;
    try
    {
      text = obj.Name;
    }
    catch (Exception ex)
    {
    }
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    bool flag = this._filter.DoesPassFilter(obj);
    errorMessage = (string) null;
    if (!flag)
    {
      errorMessage = string.Format(this._invalidObjMessage, obj != null ? (object) obj.Name : (object) string.Empty);
    }
    else
    {
      flag = this.IsUniqueObjectPicked(obj);
      if (!flag)
        errorMessage = this._duplicateErrorMessage;
    }
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    errorMessage = (string) null;
    bool flag = false;
    if (text == "")
      errorMessage = this._missingObjMessage;
    else if (this.GetObject(text) != null)
      flag = true;
    else
      errorMessage = string.Format(this._invalidObjMessage, (object) text);
    return flag;
  }
}
